<template>
  <div v-show="flag">
  <div class="search_page">
    <div class="ipt-box">
      <div class="box">
        <!-- 搜索icon -->
        <div class="iconfont icon-sousuo"></div>

        <div class="ipt">
          <input
            type="text"
            placeholder="震国神帅"
            v-model="value"
            @keyup.enter="addList"
          />
        </div>
      </div>
      <span @click="goBack">取消</span>
    </div>

    <!-- 热门搜索 -->
    <div class="hot_search" v-show="!text">
      <div>热门搜索</div>
      <ul>
        <li
          @click="hotSearchFun(item.id, item.name)"
          v-for="(item, index) in hotSearchList"
          :key="index"
        >
          <span class="sort_num">{{ index + 1 }}</span>
          {{ item.name }}
        </li>
      </ul>
    </div>

    <!-- 最近搜索 -->
    <div class="recently_search" v-show="!text">
      <div class="title_text">
        <div>历史记录</div>
        <div class="iconfont icon-shanchu" @click="del" ></div>
      </div>
      <ul>
        <li v-for="(item, index) in historyList" :key="index">
          {{ item.title }}
        </li>
      </ul>
    </div>

    <!-- 搜索出来的书籍 -->
    <div v-show="text" class="bookList">
      <div class="hotWeek">
        <div class="content">
          <div
            class="item"
            v-for="(item, index) in booksLists"
            :key="index"
            @click="goDetail(item.bookId,item.title)"
          >
            <div class="img">
              <img :src="item.cover" alt="" v-lazy="item.cover" />
            </div>
            <div class="detail">
              <div class="title">{{ item.title }}</div>
              <div class="author">{{ item.author }}</div>
              <div class="recomContent">
                {{ item.summary }}
              </div>
            </div>
          </div>
        </div>
      </div>
    </div>
  </div>
  </div>
</template>

<script>
import { getHotWords, getSearchBook } from "../api/search";
export default {
  name: "search",
  data() {
    return {
      value: "", // 输入框关键字
      historyList: [], //存放历史搜索记录
      text: "",
      hotSearchList: [], //热门搜索
      booksLists: [], // 搜索出来的书籍列表
      flag:false,
    };
  },
  watch: {
    value() {
      if (this.value === "") {
        this.text = "";
      }
    },
  },
  methods: {
    // 搜索
    addList() {
    

      this.text = this.value;
      this.historyList.unshift({
        title: this.value,
      });
      getSearchBook({ keyword: this.value }).then((data) => {
        console.log(data.data.books);
        this.booksLists = data.data.books;


       
      });
    },

    del() {
      this.historyList = [];
    },

    hotSearchFun(id, title) {
      console.log(title);
      this.$set(this.historyList, 0, { title, id });

      // 调用vuex存储状态
      this.$store.commit("setBookInfo", { id, title });
      this.$router.push({ name: "introduce" });
    },

    // 调用热门搜索接口
    getHotWordsFun() {

        this.$toast.loading({
        message: "加载中...",
        overlay: true,
      });
      getHotWords().then((data) => {
        console.log(data);
        // 拿到热门搜索数据 数组
        this.hotSearchList = data.list;


         if (this.hotSearchList.length !=0) {
          console.log(111);
          this.flag = true;
          this.$toast.clear();
          
        }
      });
    },

    goBack(){
      this.value = ""
      this.$router.go(-1)
    },

    // 点击书籍跳到详情页
    goDetail(id,title){
        // 调用vuex存储状态
      this.$store.commit("setBookInfo", { id, title });
      this.$router.push({ name: "introduce" });
    }
  },

  created() {
    this.getHotWordsFun();
  },
  activated() {
    if(this.hotSearchList.length == 0){
    this.getHotWordsFun();

    }
  },
};
</script>

<style lang="less">
.search_page {
  width: 100%;
  
  box-sizing: border-box;
  background-color: #f7f7f7;
  height: 100vh;
  .ipt-box {
    width: 325px;
    height: 26px;
    margin: 0 auto;
    padding: 10px 0;
    position: relative;
    .box {
      width: 270px;
      height: 100%;
      display: flex;
      border: 1px solid #dddddd;
      border-radius: 10px;
      background-color: #efefef;
      line-height: 30px;

      .icon-sousuo {
        margin-left: 15px;
        margin-right: 12px;
      }
      .ipt {
        line-height: 30px;
      }
      .ipt input {
        height: 20px;
        border: none;
        background-color: #efefef;
        outline: none;
        font-size: 18px;
      }
    }
    span {
      position: absolute;
      top: 40%;
      right: 0;
    }
  }
  .hot_search {
    width: 100%;
    //   font-size: 15px;
    padding: 0 15px;
    padding-top: 30px;
    box-sizing: border-box;
    background-color: #f7f7f7;
    ul {
      width: 100%;
      display: flex;
      flex-wrap: wrap;
      margin-top: 20px;
      li {
        padding-left: 10px;
        box-sizing: border-box;
        width: 50%;
        font-size: 15px;
        margin-bottom: 15px;
        .sort_num {
          width: 16px;
          height: 16px;
          border: 1px solid #dddddd;
          border-radius: 50%;
          text-align: center;
          line-height: 16px;
          font-size: 12px;
          display: inline-block;
          margin-right: 10px;
        }
        &:nth-child(1) span {
          color: white;
          background-color: #d64a2c;
        }
        &:nth-child(2) span {
          background-color: #fc7a1b;
        }
        &:nth-child(3) span {
          background-color: #fbb400;
        }
      }
    }
  }
  .recently_search {
    width: 100%;
    padding: 0 15px;
    padding-top: 30px;
    box-sizing: border-box;
    background-color: #f7f7f7;
    .title_text {
      display: flex;
      justify-content: space-between;
      .icon-shanchu{
        font-size: 20px;
      }
    }
    ul {
      display: flex;
      margin-top: 20px;
      height: 100%;
      flex-wrap: wrap;
      li {
        border: 1px solid #dddddd;
        padding: 4px 10px;
        margin-right: 5px;
        border-radius: 8px;
        font-size: 15px;
        margin-bottom: 10px;
      }
    }
  }

  // 搜索出来的
  .bookList {
    padding: 0 12px;
    padding-top: 15px;
    background: rgb(247, 247, 247);
    .hotWeek {
      border-bottom: 1px solid #bebebe;
      margin-bottom: 20px;

      .content {
        .item {
          display: flex;
          margin-bottom: 15px;
          .img {
            width: 70px;
            height: 97px;
            margin-right: 8px;
            img {
              width: 70px;
              height: 97px;
            }
          }
          .detail {
            width: 100%;
            
            text-overflow: ellipsis;
            overflow: hidden;
            .title {
              width: 100%;
              margin-bottom: 10px;
              font-weight: bold;
              line-height: 18px;

              white-space: nowrap;
              text-overflow: ellipsis;
              overflow: hidden;
            }
            .author {
              width: 100%;
              font-size: 14px;
              margin-bottom: 10px;
              line-height: 14px;
              color: #7c7c7c;

              white-space: nowrap;
              text-overflow: ellipsis;
              overflow: hidden;
            }
            .recomContent {
              width: 100%;
              // height: 30px;
              overflow: hidden;
              font-size: 14px;
              line-height: 15px;
              color: #7c7c7c;

              overflow: hidden;
              text-overflow: ellipsis;
              display: -webkit-box;
              -webkit-line-clamp: 2;
              -webkit-box-orient: vertical;
            }
          }
        }
      }
    }
  }
}
</style>